package com.prd.controller;


import com.prd.dto.WorkOrderDTO;
import com.prd.service.WorkOrderService;
import com.prd.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 工作单数据表 前端控制器
 * </p>
 *
 * @author lizhaoyi
 * @since 2023-10-30
 */
@Api(tags = {"工作单"})
@RestController
@RequestMapping("/api/workOrder")
public class WorkOrderController {

    private WorkOrderService workOrderService;

    public WorkOrderController(WorkOrderService workOrderService) {
        this.workOrderService = workOrderService;
    }

    @ApiOperation("分页查询工作单")
    @GetMapping("page")
    public JsonData pageOutsideList(
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示多少条") @RequestParam(value = "size", defaultValue = "10") int size,
            @ApiParam(value = "工作单状态0-草稿1-待执行2-未安排3-执行中4-重新执行5-预完成6-已完成7-已撤销") Integer status,
            @ApiParam(value = "工作单时间范围查询-开始时间") LocalDate startTime,
            @ApiParam(value = "工作单时间范围查询-结束时间") LocalDate endTime) {

        Map<String, Object> pageResult = workOrderService.page(page, size,status, startTime,endTime);

        return JsonData.buildSuccess(pageResult);

    }

    /**
     * 新建工作单
     *
     * @param workOrderDTO
     */
    @ApiOperation("新建工作单")
    @PostMapping("new")
    public JsonData newWorkOrder(@Validated @RequestBody WorkOrderDTO workOrderDTO) {
        return workOrderService.newWordOrder(workOrderDTO);
    }

    @ApiOperation("查看工作单详情")
    @GetMapping("detail")
    public JsonData detail(@ApiParam(value = "工作单id") @RequestParam(value = "id") @NotNull Long id) {

        return workOrderService.getDetail(id);

    }

    @ApiOperation("发送信息给负责人")
    @PostMapping("sendMessagesToLeader")
    public JsonData sendMessagesToLeader(@ApiParam(value = "工作单id") @RequestParam("id") Long id, @RequestParam("header") Long header) {
        //TODO 后面再开发

        return null;
    }

    @ApiOperation("获取某天某状态下的工作单记录")
    @GetMapping("getWorkOrderByDateAndStatus")
    public JsonData getWorkOrderByDateAndStatus(@ApiParam(value = "工作单状态") @RequestParam(value = "status") Integer status,
                                                @ApiParam(value = "时间") @RequestParam(value = "date") String date) {

        return workOrderService.getWorkOrderByDateAndStatus(status, date);
    }

    @ApiOperation("修改完成时间")
    @PostMapping("modifyCompleteTime")
    public JsonData modifyCompleteTime(@ApiParam(value = "工作单id") @RequestParam("id") Long id
            , @ApiParam(value = "修改时间") @RequestParam("time") LocalDateTime time
            , @ApiParam(value = "工作单状态") @RequestParam("status") Integer status) {

        return workOrderService.modifyCompleteTime(id, time, status);
    }

    @ApiOperation("撤销工作单")
    @PostMapping("cancel")
    public JsonData cancleWorkOrder(@ApiParam(value = "工作单id") @RequestParam("id") Long id) {

        return workOrderService.cancel(id);
    }

    @ApiOperation("增加抄送人")
    @PostMapping("addCCTo")
    public JsonData addCCTo(@ApiParam(value = "工作单id") @RequestParam("id") Long id
            , @ApiParam(value = "抄送人id") @RequestParam("userId") Long userId) {

        return workOrderService.addCCTo(id, userId);
    }

    @ApiOperation("安排执行人/由我执行")
    @PostMapping("setExecuteBy")
    public JsonData setExecuteBy(@ApiParam(value = "工作单id") @RequestParam("id") Long id
            , @ApiParam(value = "执行人id") @RequestParam("userId") Long userId) {

        return workOrderService.setExecutor(id, userId);
    }

    @ApiOperation("移除执行人")
    @PostMapping("cancelExecutor")
    public JsonData cancelExecutor(@ApiParam(value = "工作单id") @RequestParam("id") Long id) {

        return workOrderService.cancelExecutor(id);
    }

    @ApiOperation("预完成")
    @PostMapping("preComplete")
    public JsonData preComplete(@ApiParam(value = "工作单id") @RequestParam("id") Long id) {

        return workOrderService.preComplete(id);
    }

    @ApiOperation("决算")
    @PostMapping("finalAccount")
    public JsonData finalAccount(@ApiParam(value = "工作单对象") @RequestBody WorkOrderDTO workOrderDTO) {

        return workOrderService.finalAccount(workOrderDTO);
    }

    @ApiOperation("补充执行汇报")
    @PostMapping("addExecutionReport")
    public JsonData addExecutionReport(@ApiParam(value = "工作单对象") @RequestBody WorkOrderDTO workOrderDTO) {

        return workOrderService.addExecutionReport(workOrderDTO);
    }

    @ApiOperation("安排督导人员/由我督导")
    @PostMapping("setWatcher")
    public JsonData setWatcher(@ApiParam(value = "工作单id") @RequestParam("id") Long id
            , @ApiParam(value = "执行人id") @RequestParam("userId") Long userId) {

        return workOrderService.setWatcher(id, userId);
    }

    @ApiOperation("未达标/不合格")
    @PostMapping("notStandard")
    public JsonData notStandard(@ApiParam(value = "工作单对象") WorkOrderDTO workOrderDTO) {

        return workOrderService.notStandard(workOrderDTO);
    }

    @ApiOperation("任务完成")
    @PostMapping("complete")
    public JsonData complete(@ApiParam(value = "工作单id") @RequestParam("id") Long id) {

        return workOrderService.complete(id);
    }

    @ApiOperation("查询时间节点记录")
    @GetMapping("selectWorkTimeNodeList")
    public JsonData selectWorkTimeNodeList(Long id) {

        return workOrderService.selectWorkTimeNodeList(id);
    }

    @ApiOperation("预决算意见")
    @PostMapping("budgetOpinion")
    public JsonData budgetOpinion(@ApiParam(value = "工作单对象") @RequestBody WorkOrderDTO workOrderDTO) {

        return workOrderService.budgetOpinion(workOrderDTO);
    }


}

